/*
Author: 	huangjb.cn@gmail.com
Time:   	2010-07-05
Function:
    copy a string to clipboard
Usage:
    $( buttonObject ).xincopy( funtionGetText );
Example:
    $( "#idBtnCopy" ).xincopy( function(){
	return $( "idInputText" ).attr( "value" );
    } );
Test:
    tested in firefox 3.6.6
    ie 7.0.6001.18000
    google chrome 5.0.375.99
*/
if( window.XINCopySettings == undefined ){
    window.XINCopySettings = {};
}
function XINCopyGetContent( id ){
    var v = window.XINCopySettings[id]["fnGetContent"]();
    return v;
}
function XINCopyEvent( id, ev ){
    var btnObj = window.XINCopySettings[id]["btnObj"];
    if( ev == "mouseOver" ){
        btnObj.addClass( "hover" );
    }else if( ev == "mouseLeave" ){
        btnObj.removeClass( "hover" );
    }else if( ev == "mouseDown" ){
        btnObj.addClass( "active" );
    }else if( ev == "mouseUp" ){
        btnObj.removeClass( "active" );
    }
}

(function($){
    $.fn.extend({
        xincopy: function( fnGetContent ){
            return this.each(function(){
                var btnObj = $(this);
                var id = "id"+parseInt( Math.random()*10000 )+parseInt( Math.random()*10000 );
                window.XINCopySettings[ id ] = {
                    "btnObj": btnObj,
                    "fnGetContent": fnGetContent
                };
                var wrap = $("<div class='XINCopyFlashWrapper'><div class='XINCopyFlashWrapperInner'></div></div>");
                wrap.appendTo( $(document.body) );
                wrap.css( "position", "absolute" );
                
                var inner = wrap.find(".XINCopyFlashWrapperInner");
                inner.attr( "id", "XINCopyFlashWrapperInner_"+id );

		var pos = btnObj.position();
		var width = btnObj.outerWidth();
		var height = btnObj.outerHeight();
		
		wrap.css( "left", pos.left+"px" );
		wrap.css( "top", pos.top+"px" );
		wrap.css( "width", width+"px" );
		wrap.css( "height", height+"px" );
                
                var flashvars = {};
                var params = {};
                var attributes = {};

                flashvars.id = id;
		flashvars.width = width;
		flashvars.height = height;
		
                params.quality = "best";
                params.allowscriptaccess = "always";
                params.wmode = "transparent";
		params.menu = false;
		params.loop = false;
		params.allowFullScreen = false;
		params.bgcolor = "#ffffff";
                attributes.id = id;

		swfobject.embedSWF(
		    "XINCopy.swf",
                    "XINCopyFlashWrapperInner_"+id, 
		    width+"px",
                    height+"px", 
		    "10.0.0",
                    "playerProductInstall.swf", 
		    flashvars,
                    params,
                    attributes
                );
            });
        }
    });	
})(jQuery);
